﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace xiaolanshu
{
    public partial class 信息插入 : Form
    {
        SqlConnection conn = SQLTools.conn;
        string fname = null;
        public 信息插入()
        {
            BackgroundImageLayout = ImageLayout.Stretch;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                fname = openFileDialog1.FileName;
                pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.ImageLocation = fname;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataRow[] dataRows = Data.lipstick_data.Select("lnum" + "='" + textBox1.Text + "'");
            if (dataRows.Length.Equals(1))
            {
                MessageBox.Show("已存在该产品，请选择\"更新\"功能");
            }
            else
            {
                DataRow row = Data.lipstick_data.NewRow();
                row["lnum"] = "l" + string.Format("{0:D3}", (Data.lipstick_data.Rows.Count + 1));
                row["lname"] = textBox2.Text;
                row["price"] = textBox3.Text;
                row["color"] = textBox4.Text;
                row["wei"] = textBox5.Text;
                row["qgp"] = dateTimePicker1.Value;
                row["photo"] = SQLTools.imageToByte(fname);
                row["clicknum"] = 0;
                Data.lipstick_data.Rows.Add(row);
                Data.updateLipData();
                MessageBox.Show("插入成功！");
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = "l" + string.Format("{0:D3}", (Data.lipstick_data.Rows.Count + 1));
        }

        private void 信息插入_Load(object sender, EventArgs e)
        {
            Data.getLipData();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            lipstick_info f = new lipstick_info();
            f.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DataRow row = Data.lipstick_data.Select("lnum" + "='" + textBox1.Text + "'")[0];

            row["lnum"] = "l" + string.Format("{0:D3}", (Data.lipstick_data.Rows.Count + 1));
            row["lname"] = textBox2.Text;
            row["price"] = textBox3.Text;
            row["color"] = textBox4.Text;
            row["wei"] = textBox5.Text;
            row["qgp"] = dateTimePicker1.Value;
            row["photo"] = SQLTools.imageToByte(fname);

            Data.updateLipData();
            MessageBox.Show("插入成功！");
        }
    }
}
